Package

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.2 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(readxl)
library(viridis)
## Carregando pacotes exigidos: viridisLite
library(baseline)
## 
## Attaching package: 'baseline'
## 
## The following object is masked from 'package:stats':
## 
##     getCall
library(patchwork)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(glue)
devtools::load_all()
## i Loading tidyspec

Import

Importing the data can be done using packages such as readr, readxl, or the base itself. It is important to keep in mind that the tidyspec package works using the tidy-data concept.

data <- readxl::read_xlsx(path = here::here("AlgGel/dados.xlsx"))


glimpse(data)
## Rows: 1,869
## Columns: 6
## $ Wn          <dbl> 399.1992, 401.1277, 403.0562, 404.9847, 406.9132, 408.8417~
## $ Alg5        <dbl> 0.709, 0.711, 0.714, 0.715, 0.717, 0.721, 0.720, 0.720, 0.~
## $ Alg5Gel0625 <dbl> 1.0704590, 0.9417525, 0.8833830, 0.8659785, 0.8535924, 0.8~
## $ Alg5Gel125  <dbl> 0.7126237, 0.5873819, 0.5286469, 0.5090636, 0.4954177, 0.4~
## $ Alge5Gel250 <dbl> 0.9081752, 0.7801194, 0.7194563, 0.7007237, 0.6882610, 0.6~
## $ Alg5Gel5    <dbl> 0.8800188, 0.7496122, 0.6890221, 0.6703591, 0.6567779, 0.6~

Visualize

The spec_smartplot and spec_smartplotly were created to present a quick visualization of the spectra based on ggplot and plotly, respectively.

data %>% spec_smartplot()

data %>% spec_smartplotly()

Transform

data %>% spec_abs2trans() %>% spec_smartplot()

First region of interest: \(1490 cm^{-1} <=\) Wavenumber \(<= 1850 cm^{-1}\)

Filter the data

The function filter from the dplyr package can be used to filter the wavenumber column.

data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850)

Smooth the data

The function spec_smooth_sga uses the Savitzky-Golay algorithm to smooth the data.

data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21)
## Carregando pacotes exigidos: signal
## 
## Attaching package: 'signal'
## The following object is masked from 'package:plotly':
## 
##     filter
## The following object is masked from 'package:dplyr':
## 
##     filter
## The following objects are masked from 'package:stats':
## 
##     filter, poly
p1 <- data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smartplot() +
  ggtitle(label = "Before")

p2 <- data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_smartplot() +
  ggtitle(label = "After")

(p1 + p2) + plot_layout(guides = "collect")

The function spec_smooth_avg also can be used to smooth.

Baseline correction

spec_blc_rollingBall function uses the rolling Ball algorithm to perform the baseline correction.

data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31)
p1 <- data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>%
  spec_smartplot() +
  ggtitle(label = "Before")

p2 <- data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_smartplot() +
  ggtitle(label = "After")

(p1 + p2) + plot_layout(guides = "collect")

Normalize the data

spec_norm_01 normalize the data to the range of 0 to 1. The maximun value of ALL samples it’s used to perform the normalization.

data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_norm_01()
p1 <- data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_smartplot() +
  ggtitle(label = "Before")

p2 <- data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_norm_01() %>% 
  spec_smartplot() +
  ggtitle(label = "After")

(p1 + p2) + plot_layout(guides = "collect")

spec_norm_minmax will normalize to a custom range but different from spec_norm_01 each sample will have it’s maximun value set as the maximum value selected.

data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_norm_minmax(min = 0,max = 1)
p1 <- data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_smartplot() +
  ggtitle(label = "Before")

p2 <- data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_norm_minmax(min = 0,max = 1) %>% 
  spec_smartplot() +
  ggtitle(label = "After")

(p1 + p2) + plot_layout(guides = "collect")

spec_norm_var can be used to normalize the date to have a standard deviation of one.

data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_norm_var() %>% 
  spec_norm_01() 
p1 <- data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_smartplot() +
  ggtitle(label = "Before")

p2 <- data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_norm_var() %>% 
  spec_norm_01() %>% 
  spec_smartplot() +
  ggtitle(label = "After")

(p1 + p2) + plot_layout(guides = "collect")

Exporting the data

You can use functions from the readr or writexl packages to export your data.

data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_norm_01() %>% 
  #spec_smartplot(xmax = 1785)
  write_csv(file = here::here("reg1_norm.csv"))

View results

reg1 <- data %>% 
  dplyr::filter(Wn >= 1490, Wn <= 1850) %>% 
  spec_smooth_sga(window = 21) %>% 
  spec_blc_rollingBall(Wn_min = 1490,Wn_max = 1850,wm = 60,ws = 31) %>% 
  spec_norm_01()
reg1 %>% 
  pivot_longer(cols = -Wn,names_to = "Hidrogel",values_to = "Abs") %>% 
  ggplot(aes(x = Wn, y = Abs, color = Hidrogel)) +
  geom_line(size = 1.4) +
  scale_x_reverse() +
  scale_color_manual(values = c("#800303", "#06147D", "#824EC2", "#054505", "#A36310")) +
  theme_bw()